package top.scsoul.voes.framework.config;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    private String baseAppPath = new ApplicationHome().getDir().getPath();

    /**
     * 把图片请求映射到本地文件夹
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String imagesLocation = "file:" + baseAppPath + "/upload/";
        registry.addResourceHandler("/upload/**")
                .addResourceLocations(imagesLocation);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //放行哪些原始域
                .allowedOrigins("*")
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE", "POTION"})
                .allowedHeaders("*")
                .exposedHeaders("*").allowCredentials(false);
    }
}
